import pyvisa
import time


class GDM8261A(object):
    SIGNAL_TYPE_VOLTAGE = 0
    SIGNAL_TYPE_CURRENT = 1

    """
    GDM8261A brief introduction
    """

    def __init__(self, gpib_addr = None, serial = None):

        rm = pyvisa.ResourceManager()
        if gpib_addr != None:
            self.dev = rm.open_resource("GPIB0::%s::INSTR" % gpib_addr)
        else:
            self.dev = rm.open_resource("ASRL6::INSTR")

    def clear(self):
        self.dev.write('*RST')
        return

    def setDetector(self, rate):
        self.dev.write('DET:RATE %s' %rate)
        return

    def getid(self):
        self.id = self.dev.query('*IDN?')
        # print(self.id)
        return self.id

    def getV(self):
        self.v = self.dev.query('MEASure:VOLTage:DC?')
        return float(self.v)

    def getI(self):
        self.I = self.dev.query('MEASure:CURRent:DC?')
        return float(self.I)

    def getName(self):
        return "keysight34410A"

    def setSignalType(self, type):
        self.type = type

    def read(self):
        v = 0
        if self.type == keysight34410A.SIGNAL_TYPE_CURRENT:
            v = self.getI()
        else:
            v = self.getV()
        v = float(v) * 1000
        return v

# dmm = GDM8261A(serial="COM6")
# print dmm.getid()
# dmm.setDetector("F")
# while True:
#     val = dmm.getV()
#     print val
#     time.sleep(0.1)